package cn.dhnep.demo.controller;

import cn.dhnep.demo.pojo.DeliveryStatistics;
import cn.dhnep.demo.pojo.Horseman;
import cn.dhnep.demo.pojo.RecyclingStatistics;
import cn.dhnep.demo.pojo.frontvo.OrderAddCount;
import cn.dhnep.demo.pojo.frontvo.OrderItemVo;
import cn.dhnep.demo.pojo.frontvo.UserAddCount;
import cn.dhnep.demo.pojo.vo.RespBean;
import cn.dhnep.demo.pojo.vo.RespPageBean;
import cn.dhnep.demo.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户与订单
 * </p>
 *
 * @author dhn
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/dhn/adminRelevant")
public class AdminRelevantController {

    @Resource
    private UserService userService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private HorsemanService horsemanService;

    @Resource
    private RecyclingStatisticsService recyclingStatisticsService;
    @Resource
    private DeliveryStatisticsService deliveryStatisticsService;


    @ApiOperation("每日平台回收和推广数据")
    @GetMapping("getYesterdayAddUserAndOrder")
    public RespBean getYesterdayAddUserAndOrder() {
        List<UserAddCount> yesterdayAddUser = userService.getYesterdayAddUser();
        List<OrderAddCount> yesterdayOrder = orderService.getYesterdayOrder(null);
        List<OrderItemVo> yesterdayOrderItem = orderItemService.getYesterdayOrderItem(null);

        //layui的表格渲染只支持接收数组形式。。。
        List<RecyclingStatistics> list = new ArrayList<>();
        RecyclingStatistics statistics = new RecyclingStatistics(); //借用这个实体类渲染数据
        list.add(statistics);
        statistics.setAllUser(userService.count(null))
                .setStatisticsTime(new Date(new Date().getTime() - 86400000L)); //累积用户总数和日期

        if (yesterdayAddUser.isEmpty()) {
            statistics.setId(0);
            statistics.setAddUser(0);
        } else {
            Integer otherAddUser = 0; //其它新增用户数，也就是其他渠道注册的而非骑手推广的
            Integer allAddUser = 0;   //全部新增用户数

            for (UserAddCount u : yesterdayAddUser) {
                allAddUser += u.getAddCount();

                if (StringUtils.isEmpty(u.getUserPhone())) { //说明不是通过骑手推广注册的用户
                    otherAddUser += u.getAddCount();
                } else { //可能是骑手推广的
                    int i = horsemanService.count(new QueryWrapper<Horseman>().eq("horseman_phone", u.getUserPhone()));
                    if (i > 0) { //用户注册是通过骑手推广的
                        //根据骑手的手机号查询累积推广的用户数
                        u.setSumCount(userService.userCount(u.getUserPhone()));
                    } else { //非骑手推广的
                        otherAddUser += u.getAddCount();
                    }
                }
            }
            statistics.setId(otherAddUser);
            statistics.setAddUser(allAddUser);
        }

        if (yesterdayOrder.isEmpty()) {
            statistics.setOrderCount(0);
            statistics.setCompletedOrder(0);
            statistics.setIncompletedOrder(0);
            statistics.setWaste(0);
        } else {
            Integer countOrder = 0;       //昨日订单总量
            Integer completedOrder = 0;   //已完成订单量
            Integer incompleteOrder = 0;  //未完成订单量
            Integer excepOrder = 0;       //异常订单量
            for (OrderAddCount o : yesterdayOrder) {
                countOrder += o.getOrderCount();

                String orderState = o.getOrderState();
                if (orderState.equals("已完成")) {
                    completedOrder += o.getOrderCount();
                } else if (orderState.equals("异常订单")) {
                    excepOrder += o.getOrderCount();
                } else {
                    incompleteOrder += o.getOrderCount();
                }
            }
            statistics.setOrderCount(countOrder);
            statistics.setCompletedOrder(completedOrder);
            statistics.setIncompletedOrder(incompleteOrder);
            statistics.setWaste(excepOrder);
        }

        if (yesterdayOrderItem.isEmpty()) {
            statistics.setAllWeight(0.00);
            statistics.setAllMoney(0.00);
        } else {
            Double allWeight = 0.0; //昨日收货总重量
            Double allMoney = 0.0;  //昨日全部收益
            for (OrderItemVo it : yesterdayOrderItem) {
                allWeight += it.getFinallyWeight();
                allMoney += it.getSum();
                switch (it.getProductType()) {
                    case "塑料类":
                        statistics.setPlastic(it.getFinallyWeight()).setPlasticMoney(it.getSum());
                        break;
                    case "废纸类":
                        statistics.setPaper(it.getFinallyWeight()).setPaperMoney(it.getSum());
                        break;
                    case "纺织物":
                        statistics.setTextiles(it.getFinallyWeight()).setTextilesMoney(it.getSum());
                        break;
                    case "金属类":
                        statistics.setMetal(it.getFinallyWeight()).setMetalMoney(it.getSum());
                        break;
                    case "其他类":
                        statistics.setElectrical(it.getAmount()).setElectricalMoney(it.getSum());
                        break;
                }
            }
            statistics.setAllWeight(allWeight);
            statistics.setAllMoney(allMoney);
        }
        return RespBean.success(null, list);
    }

    @ApiOperation("每日骑手回收数据和推广数据统计")
    @GetMapping("recyclingStatistics")
    public RespPageBean getRecycling(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(value = "date", required = false) String date) {
        QueryWrapper<RecyclingStatistics> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(date)) wrapper.eq("statistics_time", date);
        wrapper.orderByDesc("id");
        IPage<RecyclingStatistics> iPage = recyclingStatisticsService.page(new Page<>(page, limit), wrapper);
        return new RespPageBean(0, iPage.getTotal(), iPage.getRecords());
    }


    @ApiOperation("每日骑手交货数据统计")
    @GetMapping("deliveryStatistics")
    public RespPageBean getDelivery(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(value = "date", required = false) String date) {
        QueryWrapper<DeliveryStatistics> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(date)) wrapper.eq("statistics_time", date);
        wrapper.orderByDesc("id");
        IPage<DeliveryStatistics> iPage = deliveryStatisticsService.page(new Page<>(page, limit), wrapper);
        return new RespPageBean(0, iPage.getTotal(), iPage.getRecords());
    }

}
